package com.sphd.Restaurant.fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.activity.AddressActivity;
import com.sphd.Restaurant.activity.LoginActivity;
import com.sphd.Restaurant.activity.MyDataActivity;
import com.sphd.Restaurant.activity.MyMsgActivity;
import com.sphd.Restaurant.activity.RegisterActivity;
import com.sphd.Restaurant.activity.SettingActivity;
import com.sphd.Restaurant.base.BaseFragment;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.RoundImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Hu on 2016/9/25.
 */

public class SettingFragment extends BaseFragment {
    @BindView(R.id.bt_login)
    Button btLogin;
    boolean isLogin = false;
    @BindView(R.id.iv_headportrait)
    RoundImageView ivHeadportrait;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_myhand)
    LinearLayout llMyhand;
    @BindView(R.id.tv_mySetting)
    TextView tvMySetting;
    @BindView(R.id.tv_myData)
    TextView tvMyData;
    @BindView(R.id.tv_changePassword)
    TextView tvChangePassword;
    @BindView(R.id.tv_affirm_address)
    TextView tvAffirmAddress;
    @BindView(R.id.tv_myMessage)
    TextView tvMyMessage;

    @Override
    public int getLayout() {
        return R.layout.fragment_setting;
    }

    @Override
    public void initViews(View rootView) {
        ButterKnife.bind(this, rootView);


    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin = getBoolean(Constants.ISLOGIN);
        Log.e("is==", isLogin + "");
        if (isLogin) {
            btLogin.setVisibility(View.GONE);
            llMyhand.setVisibility(View.VISIBLE);
            tvName.setText(getString(Constants.USERNAME));
            setImage(ivHeadportrait, getString(Constants.USERPHOTO));
        }
    }

    @OnClick({R.id.bt_login, R.id.tv_myData, R.id.tv_changePassword, R.id.tv_affirm_address, R.id.tv_myMessage, R.id.tv_mySetting})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.bt_login:
                goToLogin();
                break;
            case R.id.tv_myData:
                if (isLogin) {
                    bundle.putString(Constants.TAG, context.getClass().getName());
                    startActivity(MyDataActivity.class,bundle);
                } else {
                    goToLogin();
                }
                break;
            case R.id.tv_changePassword:
                if (isLogin) {
                    //注册
                    bundle.putInt(Constants.TYPE, 2);
                    bundle.putString(Constants.TAG, context.getClass().getName());
                    startActivity(RegisterActivity.class,bundle);
                } else {
                    goToLogin();
                }
                break;
            case R.id.tv_affirm_address:
                if (isLogin) {
                    bundle.putString(Constants.TAG, context.getClass().getName());
                    startActivity(AddressActivity.class, bundle);
                } else {
                    goToLogin();
                }
                break;
            case R.id.tv_myMessage:
                if (isLogin) {
                    startActivity(MyMsgActivity.class);
                } else {
                    goToLogin();
                }
                break;
            case R.id.tv_mySetting:
                if (isLogin) {
                    startActivity(SettingActivity.class);
                } else {
                    goToLogin();
                }
                break;
        }
    }

    private void goToLogin() {
        if (!isLogin) {
            startActivity(LoginActivity.class);
        }
    }
}
